package cn.ictgu.parse;

import cn.ictgu.config.OtherParseConfig;
import cn.ictgu.dto.VideoInfo;
import cn.ictgu.dao.model.Juji;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Log4j
public abstract class VideoParse {
  private final String UA_PHONE = "Mozilla/5.0 (Linux; Android 4.3; Nexus 10 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Safari/537.36";
  private final String UA_PC = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";

  public abstract VideoInfo parseVideoFromUrl(String videoUrl);

  public abstract List<Juji> parseJujiFromUrl(String videoUrl);

  protected Document getDocument(String url){
    Document document = new Document("http://w2here.com");
    try {
      document = Jsoup.connect(url).userAgent(UA_PHONE).ignoreContentType(true).get();
    } catch (IOException e) {
      log.info("网址请求失败：" + url);
    }
    return document;
  }

  protected Document getDocument(String url, String cookie){
    Document document = new Document("http://w2here.com");
    try {
      document = Jsoup.connect(url).userAgent(UA_PHONE).header("Cookie", cookie).ignoreContentType(true).get();
    } catch (IOException e) {
      log.info("网址请求失败：" + url);
    }
    return document;
  }

  protected Document getDocumentWithNoUA(String url){
    Document document = new Document("http://w2here.com");
    try {
      document = Jsoup.connect(url).ignoreContentType(true).get();
    } catch (IOException e) {
      log.info("网址请求失败：" + url);
    }
    return document;
  }


  protected Document getDocumentWithPCUA(String url) {
    Document document = new Document("http://www.youku.com");
    try {
      document = Jsoup.connect(url).userAgent(UA_PC).timeout(60 * 1000).ignoreContentType(true).get();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return document;
  }

  protected String getPhoneUA(){
    return this.UA_PHONE;
  }

  protected VideoInfo createVideoInfoWithCopyRight(String provider, String videoUrl, String parserName, String parser){
    VideoInfo videoInfo = new VideoInfo();
    videoInfo.setResource(videoUrl);
    videoInfo.setProvider(provider);
    videoInfo.setParser(parser);
    videoInfo.setParserName(parserName);
    return videoInfo;
  }

  protected String commenParse(String videoUrl){
    Document document = getDocument(OtherParseConfig.COMMEN + videoUrl);
    Matcher matcher = Pattern.compile("(\\{\"id\":.*?\\})").matcher(document.html());
    if (matcher.find()){
      JSONObject json = JSONObject.parseObject(matcher.group(1));
      String id = json.getString("id");
      String md5 = json.getString("md5");
      try {
        document = Jsoup.connect(OtherParseConfig.COMMEN_API).data("id", id).data("type", "auto").data("siteuser", "").data("md5", md5).userAgent(getPhoneUA()).post();
        JSONObject playJson = JSONObject.parseObject(document.text());
        String playUrl = playJson.getString("url");
        return URLDecoder.decode(playUrl, "utf-8");
      } catch (IOException e) {
        log.info("POST失败：" + OtherParseConfig.COMMEN_API + " id: " + id + " md5: " + md5);
      }
    }
    return "";
  }
}